package com.article_main.search;

import com.article_protocol.pojo.entity.Article;
import com.article_protocol.pojo.po.ArticleSearchPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ArticleSearchRepositoryImpl implements IArticleSearchRepository{
    @Autowired
    @Lazy
    private ArticleSearchRepository articleSearchRepository;

    @Override
    public void saveArtcile(ArticleSearchPO articleSearchPO) {
        log.info("开始执行向搜索服务器写入文章数据");
        articleSearchRepository.save(articleSearchPO);
    }

    @Override
    public void updateArticle(ArticleSearchPO articleSearchPO) {
        log.info("开始执行更新文章数据");
        articleSearchRepository.save(articleSearchPO);
    }

    @Override
    public void deleteArticleById(Integer articleId) {
        log.info("开始执行删除文章数据");
        articleSearchRepository.deleteById(articleId);
    }

    @Override
    public List<ArticleSearchPO> searchArticleByKeyword(String keyword) {
        // 创建ArticleSearchPO对象
        ArticleSearchPO searchPO = new ArticleSearchPO();
        // 将keyword转换为字符串数组
        String[] fields = {keyword};
        // 创建分页参数
        Pageable pageable = PageRequest.of(0, 10);
        // 调用正确的searchSimilar方法
        return articleSearchRepository.searchSimilar(searchPO, fields, pageable).getContent();
    }

    @Override
    public List<Article> search(String keyword) {
        log.info("开始执行向搜索服务器写入文章数据");
        return null;
    }





}
